<?php
/**
 * articles actions.
 *
 * @package    IPLIFE
 * @subpackage articles
 * @author     alexey.shapran@gmail.com
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class articlesActions extends sfActions
{
	/**
	 * Executes index action
	 *
	 * @param sfRequest $request A request object
	 */
	public function executeIndex(sfWebRequest $request)
	{
		$this->articles = Article::getLastArticles(sfConfig::get('app_max_per_page',10));
		$this->articles_ukr = Article::getLastArticlesUkr(sfConfig::get('app_max_per_page',10));
		$this->setTemplate('category');
		
		$response = $this->getResponse();		
		$response->addMeta('keywords', SettingsData::Get_it(SettingsData::ART_KEY));
		$response->addMeta('description', SettingsData::Get_it(SettingsData::ART_DESC));
		$response->setTitle(sfConfig::get('app_title')." | ".SettingsData::Get_it(SettingsData::ART_TITLE)  );
	}

	/**
	 * Get View of selected article's category
	 *
	 * @author	Alexey Shapran <alexey.shapran@gmail.com>
	 * @param sfRequest $request A request object [category_alias]
	 */
	public function executeCategory(sfWebRequest $request) 
	{
		$Category = $this->getRoute()->getObject();
		$this->forward404Unless($Category instanceof ArticleCategory);

		$query = Doctrine_Query::create()
			->from('article a')
			->leftJoin('a.ArticleCategory category')
			->orderBy('a.updated_at DESC')
			->limit(sfConfig::get('app_max_per_page',10))
			->where("category_id = ?", array($Category->id));
		$this->articles = $query->execute();

		$query = Doctrine_Query::create()
			->from('article a')
			->leftJoin('a.ArticleCategory category')
			->orderBy('a.updated_at DESC')
			->limit(sfConfig::get('app_max_per_page',10))
			->where("category_id = ?", array($Category->id))
			->andWhere('a.is_ukraine=1');
		$this->articles_ukr = $query->execute();
	}
	
	/**
	 * Get View of selected article
	 *
	 * @author	Alexey Shapran <alexey.shapran@gmail.com>
	 * @param sfRequest $request A request object
	 */
	public function executeView(sfWebRequest $request) 
	{
		$this->article = $this->getRoute()->getObject();		
		$this->forward404Unless($this->article);

		$response = $this->getResponse();
		$response->addMeta('keywords', $this->article['keywords']);
		$response->addMeta('description', $this->article['description']);
		$response->setTitle(sfConfig::get('app_title')." | ".$this->article['title']); 
	}

	/**
	 * Get list of articles by Tagname
	 *
	 * @author	Alexey Shapran <alexey.shapran@gmail.com>
	 * @param sfRequest $request A request object
	 */
	public function executeTags(sfWebRequest $request) 
	{
		$this->tag = rtrim(ltrim($request->getParameter('tag')));
		$this->forward404Unless($this->tag);

		$this->articles = Article::getByTag($this->tag);
	}
}
